/*
** remote_write.c for pud in mrcellophane/libpud
** 
** Made by solomon hykes
** Login   <torgau@gmail.com>
** 
** Started on  Mon May 16 23:49:58 2005 solomon hykes
** Last update Sun Jun  5 02:57:29 2005 solomon hykes
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pud.h"

int	main(int argc, char **argv)
{
  int		pid;
  char		buf[1024];
  int		r;

  if (argc < 2)
    {
      fprintf(stderr, "Usage: %s pid\n\nRead executable code from stdin, then inject it and run it on the target>\n", argv[0]);
      exit(1);
    }
  pid = atoi(argv[1]);
  r = read(0, buf, sizeof(buf));
  if (r < 0)
    {
      perror("read");
      exit(1);
    }
  printf("Injecting %d bytes of code...\n", r);
  pscode(pid, buf, r);
}

int	pscode(int pid, void *code, u_long codeLen)
{
  pud_process	*proc;
  pud_process	*procSave;
  pud_event	*event;
  int		ret;

  proc = pud_attach(pid);
  if (proc == NULL)
    {
      perror("pud_attach");
      exit(1);
    }
  event = pud_next_event(proc);
  if (event == NULL)
    {
      perror("pud_next_event");
      pud_detach(proc);
      exit(1);
    }
  procSave = pud_dup(proc);
  pud_code_exec(proc, code, codeLen);
  //  pud_set_state(proc, procSave);
  pud_release(procSave);
  pud_detach(proc);
  return (ret);
}
